package org.dyk.seamanshousefrontdesk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.dyk.seamanshousefrontdesk.config.AliPayConfig;
import org.dyk.seamanshousefrontdesk.model.Cart;
import org.dyk.seamanshousefrontdesk.model.Goods;
import org.dyk.seamanshousefrontdesk.model.Orders;
import org.dyk.seamanshousefrontdesk.model.User;
import org.dyk.seamanshousefrontdesk.service.cart.CartService;
import org.dyk.seamanshousefrontdesk.service.goods.GoodsService;
import org.dyk.seamanshousefrontdesk.service.orders.OrdersService;
import org.dyk.seamanshousefrontdesk.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = {"/orders"})
public class OrdersController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户订单页面
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = {""})
    public String orders(HttpSession session,Model model){
        JSONArray jsonArray = new JSONArray();
        User userData = (User) session.getAttribute(Constants.USER_SESSION);
        List<Orders> allByUid = ordersService.findAllByUid(userData.getUid());
        for (Orders orders:allByUid) {
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(ordersService.findByOid(orders.getOid()));
            jsonObject.put("goodsName",goodsService.findByGid(orders.getGid()).getGoodsName());
            jsonArray.add(jsonObject);
        }
        model.addAttribute("ordersAll", jsonArray);
        return "orders";
    }

    /**
     * 确认订单页面
     * @return
     */
    @RequestMapping("/confirm")
    public String confirm(@RequestParam int cid, Model model){
        //获取购物车信息
        Cart cart = cartService.findByCid(cid);
        //获取商品信息，显示在订单页面上
        Goods goods = goodsService.findByGid(cart.getGid());
        JSONObject jsonObject = (JSONObject) JSON.toJSON(goods);
        jsonObject.put("cid", cart.getCid());
        jsonObject.put("quantity", cart.getQuantity());
        model.addAttribute("data",jsonObject);
        return "confirm";
    }

    /**
     * 添加订单
     * @param cid
     * @param quantity
     * @param price
     * @param consignee
     * @param phone
     * @param address
     * @param session
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public boolean insert(@RequestParam int cid,
                      @RequestParam int quantity,
                      @RequestParam int price,
                      @RequestParam String consignee,
                      @RequestParam String phone,
                      @RequestParam String address,
                      HttpSession session){
        //添加订单，跳转到支付页面
        User userData = (User) session.getAttribute(Constants.USER_SESSION);
        boolean insert = ordersService.insert(cid, userData.getUid(), quantity, price, consignee, phone, address);
        if (insert){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 更改订单状态
     * @param oid
     * @return
     */
    @RequestMapping("/updata")
    public String updata(@RequestParam int oid){
        //确认收货操作
        boolean updata = ordersService.updata(oid);
        if (updata){
            return "redirect:/orders";
        }else {
            return "redirect:/orders";
        }
    }
}
